use sync_clipboard::sessions::{Session};
use tokio::net::TcpListener;
use std::sync::Arc;
use tokio::sync::Mutex;
use tokio::sync::broadcast;
#[tokio::main]
async fn main() {
    // id -> sessions
    // let users: Arc<Mutex<Vec<User>>> = Arc::new(Mutex::new(vec![]));
    let listener = TcpListener::bind("0.0.0.0:20505").await.unwrap();
    println!("Listening on :20505");
    let (tx , _ ) = broadcast::channel(100);
    let tx = Arc::new(tx);
    while let Ok((stream, _)) = listener.accept().await {
        // let users_clone = users.clone();
        let tx_clone = tx.clone();
        let rx = tx_clone.subscribe();
        tokio::spawn(async move {
            let session: Arc<Mutex<Session>> =Arc::new(Mutex::new(  Session::new(stream, 0, "test".to_string()).await )) ;
            // let user = User::new(0, "x_wq3337".to_string(), "123456".to_string(), vec![session.lock().await.clone()]);
            // { 
            //     let mut users_clone = users_clone.lock().await;
            //     users_clone.push(user);
            //     println!("{:?}", users_clone);
            // }
            session.lock().await.start_loop(rx,tx_clone).await;
        });
    }
}